class Solution {
public:
    int stockManagement(vector<int>& rotateArray) {
        if (rotateArray.empty())
        {
            return 0;
        }
        int left = 0, right = rotateArray.size() - 1, mid = 0;
        while (left < right)
        {
            if (right - left == 1) // 两个下标已经相邻了
            {
                mid = right;
                break;
            }
            mid = left + ((right - left) >> 1); // 注意操作符优先级
            if (rotateArray[mid] == rotateArray[left] && rotateArray[left] == rotateArray[right]) 
            { // 无法判定目标数据在mid左侧，还是右侧 -> 采用线性探测方式
                int result = rotateArray[left];
                for (int i = left + 1; i < right; i++) 
                {
                    if (result > rotateArray[i]) 
                    {
                        result = rotateArray[i];
                    }
                }
                return result;
            }
            if (rotateArray[mid] >= rotateArray[left])  //说明mid在前半部分
            { //两者相等， 隐含条件rotateArray[left] >= rotateArray[right]
                left = mid;
            }
            else  // 说明mid在后半部分
            {
                right = mid;
            }
        }
        return rotateArray[mid];
    }
};